export default {
  name: "index",
  mounted() {
    this.getJobPage(1, 10);
    this.getEnvList();
    this.getGroupList();
  },
  data() {
    return {
      detailId:0,
      jobDetail: {},
      title: "添加任务",
      statusModel: 0,
      cronExpression: "",
      emailList: "",
      isSendEmail: false,
      isSendDingDing: false,
      input: "",
      groupIds: [],
      groupList: [],
      envId: "",
      envList: [],
      jobList: {
        data: {
          list: [
            {
              id: "",

            }
          ],
          pageNumber: 0,
          pageSize: 0,
        }
      },
      ta: 50,
      categoryList: {
        data: {
          list: [
            {
              id: "id",
              name: "name",
              remark: "remark",
              createTime: "createTime",
            },
          ],
          pageSize: 10,
          pageNumber: 1,
          total: 10,
        },
      },
      status: [
        {
          id: 1,
          name: "暂停"
        }, {
          id: 0,
          name: "开启"
        }
      ],
      dialogFormVisible: false,
      name: "",
      remark: "",
      formLabelWidth: "100px",

    }
  },
  watch: {
    projectId: function (newProjectId) {
      this.getJobPage(1, 10);
      this.getGroupList();
    }
  },
  computed: {
    projectId: function () {
      return this.$store.getters.getProjectId;
    },
  },
  methods: {
    search() {
    },
    getStatus(row, column) {
      if (row.status == 1) {
        return "暂停"
      } else {
        return "开启"
      }
    },
    formatter(row, column) {
      return this.$dateFomat.timestampToTime(row.createTime * 1000);
    },
    addJobPage() {
      this.title = "添加任务";
      this.getEnvList();
      this.name = "";
      this.remark = "";
      this.groupIds = [];
      this.cronExpression = "";
      this.isSendEmail = false;
      this.isSendDingDing = false;
      this.statusModel = 0;
      this.emailList = "";
    },
    getEnvList() {
      this.$env
        .list({projectId: this.$store.getters.getProjectId})
        .then((res) => {
          this.envList = res.data;
          if (this.envList.length > 0) {
            this.envId = this.envList[0].id;
          } else {
            this.envId = "";
          }
          console.log(this.envList);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    addJob() {
      if (this.title === "添加任务"){
        var emilListResult = this.emailList.toString().split(",")
         this.$job
          .jobAdd({
            projectId: this.$store.getters.getProjectId,
            jobName: this.name,
            envId: this.envId,
            status: this.statusModel,
            isSendEmail: this.isSendEmail,
            isSendDingDing: this.isSendDingDing,
            emailList: emilListResult,
            groupIdList: this.groupIds,
            cronExpression: this.cronExpression,
            remark: this.remark
          })
          .then((res) => {
            if (res.code == 200) {
              this.dialogFormVisible = false;
              this.getJobPage(this.jobList.data.pageNum, this.jobList.data.pageSize);
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((res) => {
            console.log(res);
          });
      }else {
        var emilListResult1 = this.emailList.toString().split(",")
        this.$job.jobUpdate({
          id: this.detailId,
          projectId: this.$store.getters.getProjectId,
          jobName: this.name,
          envId: this.envId,
          status: this.statusModel,
          isSendEmail: this.isSendEmail,
          isSendDingDing: this.isSendDingDing,
          emailList: emilListResult1,
          groupIdList: this.groupIds,
          cronExpression: this.cronExpression,
          remark: this.remark}).then(res=>{
          if (res.code == 200) {
            this.dialogFormVisible = false;
            this.getJobPage(this.jobList.data.pageNum, this.jobList.data.pageSize);
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message);
          }
        })
          .catch((res) => {
            console.log(res);
          });
      }

    },
    getJobPage(pageNum, pageSize) {
      this.$job
        .jobPage({
          pageNum: pageNum,
          pageSize: pageSize,
          projectId: this.$store.getters.getProjectId
        })
        .then((res) => {
          if (res.code == 200) {
            this.jobList = res;
          } else {
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    getGroupList() {
      this.$group
        .list({projectId: this.$store.getters.getProjectId})
        .then((res) => {
          if (res.code == 200) {
            this.groupList = res.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    cz() {
      this.input = "";
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.$job
        .jobPage({pageNum: 1, pageSize: val})
        .then((res) => {
          if (res.code == 200) {
            this.jobList = res;
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    handleCurrentChange(val) {
      this.$job
        .jobPage({
          pageNum: val,
          pageSize: this.jobList.data.pageSize,
        })
        .then((res) => {
          var result = res;
          if (res.code == 200) {
            this.jobList = res;
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    handleEdit(index) {
      this.detailId = this.jobList.data.list[index].id;
      this.$job.jobDetail({id: this.detailId}).then((res) => {
        if (res.code == 200) {
          this.jobDetail = res.data;
          this.name = this.jobDetail.jobName;
          this.envId = this.jobDetail.envId;
          this.statusModel = this.jobDetail.status;
          this.isSendEmail = this.jobDetail.isSendEmail;
          this.isSendDingDing = this.jobDetail.isSendDingDing;
          this.emailList = this.jobDetail.emailList.toString();
          this.groupIds = this.jobDetail.groupIdList;
          this.cronExpression = this.jobDetail.cronExpression;
          this.remark = this.jobDetail.remark;
        }
      }).catch((err) => {
        console.log(err)
      });
      this.title = "编辑任务"
      this.dialogFormVisible = true
      // this.$router.push({path: "/jobDetail", query: {id: id}});
    },
    handleRun(index) {
      var id = this.jobList.data.list[index].id;
      this.$job.jobRun({jobId: id}).then((res) => {
        if (res.code == 200) {
          this.$message({
            showClose: true,
            message: res.message,
            type: "success",
          })
        }
      }).catch((err) => {
        console.log(err);
      })
    },
    handleDelete(index) {
      var id = this.jobList.data.list[index].id;
      this.$job
        .jobDelete({id: id})
        .then((res) => {
          console.log(res);
          this.getJobPage(this.jobList.data.pageNum, this.jobList.data.pageSize);
        })
        .catch((res) => {
          console.log(res);
        });
    },
  }
}
